package com.fzh.controller.back;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fzh.pojo.entity.OrderInfo;
import com.fzh.pojo.entity.Product;
import com.fzh.pojo.entity.ProductType;
import com.fzh.pojo.utils.PageUtil;
import com.fzh.pojo.utils.ResponeMessageUtil;
import com.fzh.service.ImagesService;
import com.fzh.service.OrderInfoService;
import com.fzh.service.ProductService;
import com.fzh.service.ProductTypeService;

@Controller
@RequestMapping("/back/")
public class ProductController {
	
	@Autowired
	private ProductService productService;
	
	@Autowired
	private ProductTypeService productTypeService;
	
	@Autowired
	private OrderInfoService orderInfoService;
	
	@Autowired
	private ImagesService imagesService;
	
	
	/**
	 * 查询出所有的商品数据返回到页面
	 * @param page
	 * @param limit
	 * @param name
	 * @param f_ptid
	 * @param isSoldOut
	 * @param isRecommendMain
	 * @return
	 */
	@RequestMapping("/queryAllProduct")
	@ResponseBody
	public ResponeMessageUtil queryAllProduct(int page,int limit,String name,String f_ptid,String isSoldOut,String isRecommendMain) {
		Integer setValue = PageUtil.getSetValue(page, limit);
		List<Product> products = productService.queryAllProduct(setValue,limit,name,f_ptid,isSoldOut,isRecommendMain);
		return ResponeMessageUtil.ok(productService.queryProductCounts(name,f_ptid,isSoldOut,isRecommendMain), products);
	}
	
	/**
	 * 查询出商品的类型用于搜索
	 * @return
	 */
	@RequestMapping("/queryProductTypeName")
	@ResponseBody
	public List<ProductType> queryProductTypeName(){
		return productTypeService.queryProductTypeName();
	}
	
	
	/**
	 * 添加商品的信息
	 * @param isSoldOutStatus
	 * @param isRecommendMainStatus
	 * @param isRollImgStatus
	 * @param product
	 * @return
	 */
	@RequestMapping("/addProduct")
	@ResponseBody
	public Boolean addProduct(String isSoldOutStatus,String isRecommendMainStatus,
					String isRollImgStatus,Product product) {
		if(isSoldOutStatus != null && isSoldOutStatus != "" && isSoldOutStatus.equals("on")) {
			product.setIsSoldOut(0);
		}else {
			product.setIsSoldOut(1);
		}
		if(isRecommendMainStatus != null && isRecommendMainStatus != "" && isRecommendMainStatus.equals("on")) {
			product.setIsRecommendMain(1);
		}else {
			product.setIsRecommendMain(0);
		}
		if(isRollImgStatus != null && isRollImgStatus != "" && isRollImgStatus.equals("on")) {
			product.setIsRollImg(1);
		}else {
			product.setIsRollImg(0);
		}
		return productService.addProduct(product);
	}
	
	/**
	 * 编辑商品信息根据id
	 * @param isSoldOutStatus
	 * @param isRecommendMainStatus
	 * @param isRollImgStatus
	 * @param product
	 * @return
	 */
	@RequestMapping("/editProductById")
	@ResponseBody
	public Boolean editProductById(String isSoldOutStatus,String isRecommendMainStatus,
			String isRollImgStatus,Product product) {
		if(isSoldOutStatus != null && isSoldOutStatus != "" && isSoldOutStatus.equals("on")) {
			product.setIsSoldOut(0);
		}else {
			product.setIsSoldOut(1);
		}
		if(isRecommendMainStatus != null && isRecommendMainStatus != "" && isRecommendMainStatus.equals("on")) {
			product.setIsRecommendMain(1);
		}else {
			product.setIsRecommendMain(0);
		}
		if(isRollImgStatus != null && isRollImgStatus != "" && isRollImgStatus.equals("on")) {
			product.setIsRollImg(1);
		}else {
			product.setIsRollImg(0);
		}
		return productService.editProductById(product);
	}
	
	/**
	 * 批量删除商品根据ids
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delAllProductByIds")
	@ResponseBody
	public Boolean delAllProductByIds(@RequestParam("ids")String ids) {
		return productService.delAllProductByIds(ids);
	}
	
	/**
	 * 删除单个的商品根据id
	 * @param id
	 * @return
	 */
	@RequestMapping("/delProductById/{id}")
	@ResponseBody
	public Boolean delProductById(@PathVariable("id")Integer id) {
		return productService.delProductById(id);
	}
	
	
	/**
	 * 编辑商品的是否在售状态
	 * @param id
	 * @param valid
	 * @return
	 */
	@RequestMapping("/editProductIsSoldOutSwitchById/{id}/{valid}")
	@ResponseBody Boolean editProductIsSoldOutSwitchById(@PathVariable("id")Integer id,@PathVariable("valid")String valid) {
		Integer number = 0;
		if(valid != null && valid != "" && valid.equals("on")) {
			number = 0;
		}else {
			number = 1;
		}
		return productService.editProductIsSoldOutSwitchById(id,number);
	}
	
	/**
	 * 编辑商品的是否可删除状态
	 * @param id
	 * @param valid
	 * @return
	 */
	@RequestMapping("/editProductIsDelSwitchById/{id}/{valid}")
	@ResponseBody Boolean editProductIsDelSwitchById(@PathVariable("id")Integer id,@PathVariable("valid")String valid) {
		Integer number = 0;
		if(valid != null && valid != "" && valid.equals("on")) {
			number = 1;
		}else {
			number = 0;
		}
		return productService.editProductIsDelSwitchById(id,number);
	}
	
	/**
	 * 编辑商品是否推荐首页的状态
	 * @param id
	 * @param valid
	 * @return
	 */
	@RequestMapping("/editProductIsRecommendMainSwitchById/{id}/{valid}")
	@ResponseBody Boolean editProductIsRecommendMainSwitchById(@PathVariable("id")Integer id,@PathVariable("valid")String valid) {
		Integer number = 0;
		if(valid != null && valid != "" && valid.equals("on")) {
			number = 1;
		}else {
			number = 0;
		}
		return productService.editProductIsRecommendMainSwitchById(id,number);
	}
	
	/**
	 * 编辑商品是否显示到滚动图
	 * @param id
	 * @param valid
	 * @return
	 */
	@RequestMapping("/editProductIsRollImgSwitchById/{id}/{valid}")
	@ResponseBody Boolean editProductIsRollImgSwitchById(@PathVariable("id")Integer id,@PathVariable("valid")String valid) {
		Integer number = 0;
		if(valid != null && valid != "" && valid.equals("on")) {
			number = 1;
		}else {
			number = 0;
		}
		return productService.editProductIsRollImgSwitchById(id,number);
	}
	
	/**
	 * 根据ids来查询出订单列表里面是否存在该商品,用于做批量删除操作过滤
	 * @param ids
	 * @return
	 */
	@RequestMapping("/queryAllOrderInfoByIds")
	@ResponseBody
	public Boolean queryAllOrderInfoByIds(@RequestParam("ids") String ids){
		return orderInfoService.queryAllOrderInfoByIds(ids);
	}
	
	/**XXXXXXXXXXX
	 * 根据id来查询出订单列表里面是否存在该商品,用于在商品列表做单个删除操作过滤
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryOrderInfoById/{id}")
	@ResponseBody
	public OrderInfo queryOrderInfoById(@PathVariable("id") Integer id) {
		return orderInfoService.queryOrderInfoById(id);
	}
	
	/**
	 * 查询所有的商品图片根据ids用于在商品管理页面上批量删除的商品下是否有图片链接，如有就不能直接删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/queryAllImagesByIds")
	@ResponseBody
	public Boolean queryAllImagesByIds(@RequestParam("ids")String ids) {
		return imagesService.queryAllImagesByF_pids(ids);
	}
	
	
	
}
